import 'package:flutter/material.dart';

class ChildComp extends StatefulWidget {
  const ChildComp({super.key, required this.count, required this.updateCount});

  // 定义属性，接受父组件的传值
  final int count;

  //定义回调函数,用于子组件更新父组件的值
  final Function(int) updateCount;

  @override
  _ChildCompState createState() {
    print('子组件---createState');
    return _ChildCompState();
  }
}

class _ChildCompState extends State<ChildComp> {
  @override
  void initState() {
    print('子组件---initState');
    super.initState();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print('子组件---didChangeDependencies');
  }

  @override
  Widget build(BuildContext context) {
    print('子组件---build');
    return Container(
      width: 200,
      height: 70,
      color: Colors.green,
      child: Column(
        children: [
          Text('子组件,父组件传值为：${widget.count}'),
          ElevatedButton(
              onPressed: () {
                widget.updateCount(widget.count + 5);
              },
              child: const Text('点击传值'))
        ],
      ),
    );
  }

  @override
  void didUpdateWidget(ChildComp oldWidget) {
    super.didUpdateWidget(oldWidget);
    print('子组件---didUpdateWidget');
  }

  @override
  void deactivate() {
    print('子组件---deactivate');
    super.deactivate();
  }

  @override
  void dispose() {
    print('子组件---dispose');
    super.dispose();
  }
}
